import traceback
import pymysql as mysql

db = mysql.connect("localhost", "tour", "DLnetwork.", "tour")

cursor = db.cursor()

# 数据库连接成功
# INSERT INTO `tour`.`spot` (`id`, `cnname`) VALUES ('1345', '伤害');
sql = "insert into spot(id, cnname) values(%d, '%s')"

text = sql % (5678, '我是名字dadaddada段李')
try:
   # 执行sql语句
   cursor.execute(text)
   cursor.execute(text)
   # 提交到数据库执行
   db.commit()
except mysql.err.IntegrityError:
    print('重复插入')
    db.rollback()
except Exception:
    traceback.print_exc()
    # 如果发生错误则回滚
    print('错误')
    db.rollback()

db.close()
# 数据库关闭成功